package com.oweight.sagacious.auth.server.infrastructure.configuration;

import com.oweight.sagacious.auth.server.infrastructure.interceptor.ServiceAuthRestInterceptor;
import com.oweight.sagacious.auth.server.infrastructure.interceptor.UserAuthRestInterceptor;
import com.oweight.sagacious.common.handler.GlobalExceptionHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * WebConfig
 *
 * @author 周广明
 * @version v1 2019/10/21 16:13
 */
@Slf4j
@Configuration("adminWebConfig")
@Primary
public class WebConfiguration implements WebMvcConfigurer {
    /**
     * 全局异常
     */
    @Bean
    GlobalExceptionHandler getGlobalExceptionHandler() {
        return new GlobalExceptionHandler();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        log.info("加载拦截配置...");
        registry.addInterceptor(getServiceAuthRestInterceptor()).addPathPatterns("/service/**");
        registry.addInterceptor(getUserAuthRestInterceptor()).addPathPatterns("/service/**");
    }

    /**
     * 匹配授权客户端
     */
    @Bean
    ServiceAuthRestInterceptor getServiceAuthRestInterceptor() {
        return new ServiceAuthRestInterceptor();
    }

    /**
     * 解析Token中的用户信息
     */
    @Bean
    UserAuthRestInterceptor getUserAuthRestInterceptor() {
        return new UserAuthRestInterceptor();
    }

}
